package com.kdg.thread.consurrenttest;

import java.util.concurrent.*;

/**
 * @author kdg
 * @description:
 * @date 2018/5/13.
 */
public class SemophoreTest implements Runnable{
    final Semaphore semp = new Semaphore(5);

    @Override
    public void run() {
        try {
            semp.acquire();
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getId()+" is done");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            semp.release();
        }
    }

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(20, 20,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
        SemophoreTest semophoreTest = new SemophoreTest();
        for(int i=0;i<20;i++){
            threadPoolExecutor.submit(semophoreTest);
        }
    }
}
